package yf.mall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import yf.mall.dao.MallTypeDao;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.entity.MallTypeEntity;
import yf.mall.service.MallGoodsService;
import yf.mall.service.MallTypeService;
import com.yf.service.impl.BaseServiceImpl;


/**
 * 分类Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Fri Nov 01 10:57:07 CST 2019
 */
@Service
public class MallTypeServiceImpl extends BaseServiceImpl<MallTypeEntity> implements MallTypeService {

    private MallTypeDao mallTypeDao;
    @Autowired
    private MallGoodsService mallGoodsService;


    @Autowired
    public void setDao(MallTypeDao mallTypeDao) {
        this.mallTypeDao = mallTypeDao;
        super.setBaseDao(mallTypeDao);
    }


    /**
     *
     *   @Override
     *     public List<UserVo> queryListVo(UserVo userVo) {
     *         return userDao.queryListVo(userVo);
     *     }
     *
     *     @Override
     *     public UserVo getVo(Long id) {
     *         return userDao.getVo(id);
     *     }

     @Override
     List<MallTypeVo> queryListVo(MallTypeVo mallTypeVo){
            return mallTypeDao.queryListVo(mallTypeVo);
     }

     @Override
     MallTypeVo getVo(Long id){
                return mallTypeDao.getVo(id);
    }

     **/


    /**内部方法**/



    /**外部方法实现**/

    @Override
    public List<MallTypeEntity> findByParentId(Long parentId) {
        return mallTypeDao.findByParentId(parentId);
    }

    @Override //quequnlong
    @Transactional(rollbackFor = Exception.class)
    public void saveGoodsIdAndTypeId(Long goods_id, Long type_id) {
            mallTypeDao.saveGoodsIdAndTypeId(goods_id,type_id);
    }

    @Override //quequnlong
    @Transactional(rollbackFor = Exception.class)
    public void delByGoodsId(Long goods_id) {
        mallTypeDao.delByGoodsId(goods_id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delByGoodsIdBatch(Long[] goods_id) {
        mallTypeDao.delByGoodsIdBatch(goods_id);
    }

    @Override
    public List findByGoodsId(Long goods_id) {
        List typesResult = new ArrayList();
        //本产品的二级分类ID
        List<Long> typeIds = mallTypeDao.findByGoodsId(goods_id);
        for (Long tid:typeIds){
            ArrayList list = new ArrayList();
            MallTypeEntity typeEntity = mallTypeDao.get(tid);
            list.add(typeEntity.getPid());
            list.add(tid);
            typesResult.add(list);
        }
         return  typesResult;
    }

    @Override //quequnlong
    public List<MallTypeEntity> findAll() {
        return mallTypeDao.findAll();
    }


    @Override //quequnlong
    public List<MallTypeEntity> findType(){
        List<MallGoodsEntity> goods =mallGoodsService.findGoods(); //所有产品
        List<Long> type = new ArrayList<>();  //存储产品分类id的集合
        List<MallTypeEntity> mallTypeEntities = new ArrayList<>();  //存储结果集

        //获取所有产品的分类id
        for (MallGoodsEntity good : goods) {
           // Long mallTypeId = good.getType();
            //获取产品所对应分类
            List<Long> typeIds = mallTypeDao.findByGoodsId(good.getId());
            if (null != typeIds || typeIds.size() > 0){
                    //循环
                    for (Long mallTypeId : typeIds) {
                        if (mallTypeId == null ||type.contains(mallTypeId.longValue())){
                            continue;
                        }
                        type.add(mallTypeId.longValue());
                    }
                }
        }

        if (null != type || type.size() > 0){
            for (Long typeId : type) {
                MallTypeEntity mallTypeEntity = mallTypeDao.get(typeId); //根据分类id查分类

                //是二级  去查一级
                MallTypeEntity oneType = mallTypeDao.get(mallTypeEntity.getPid());
           /* if (oneType == null){
                throw new RRException("分类不存在");
            }*/
                //判断集合是否存在该一级分类
                boolean flag = containType(mallTypeEntities, mallTypeEntity, oneType);
                if (!flag) {
                    oneType.addChildren(mallTypeEntity);
                    mallTypeEntities.add(oneType);
                }
            }
            //排序
            sort(mallTypeEntities);
        }

        return mallTypeEntities;
    }

    @Override
    public List<Long> findByType(Long type) {
        return mallTypeDao.findByTypeId(type);
    }

    //quequnlong
    private boolean containType(List<MallTypeEntity> mallTypeEntities,MallTypeEntity mallTypeEntity,MallTypeEntity oneType) {
        for (MallTypeEntity oneEntity : mallTypeEntities) {
           //如果一级分类集合存在该一级集合 就添加二级
            if (oneEntity.getId().equals(oneType.getId())){
                oneEntity.addChildren(mallTypeEntity);
                return true;
            }
        }
        return false;
    }

    //排序
    public void sort(List<MallTypeEntity> list){
        Collections.sort(list,(o1,o2) ->{
            if ( null!= o1.getSort() && null != o2.getSort()){
                if (o1.getSort() > o2.getSort()){
                    return 1;
                }
            }
                return -1;
        });
    }
}
